package timbap.visualizer.coreplugins;

import processing.core.PFont;
import processing.core.PImage;
import timbap.visualizer.api.context.PluginContext;
import timbap.visualizer.api.model.RecordItem;
import timbap.visualizer.api.plugin.recorditems.AbstractRecordItemsPlugin;

public class DefaultBrowsedRecordItemsPlugin extends AbstractRecordItemsPlugin {
	
	public final static boolean DEBUG_MODE_ENABLED = true;
	
	PFont debugFont;
	PFont defaultFont;

    @Override
    public void init(PluginContext pPluginContext) {
        super.init(pPluginContext);
        debugFont = getContext().getP5Applet().loadFont(getContext().getCfg().getDebugFont());
		defaultFont = getContext().getP5Applet().loadFont(getContext().getCfg().getStandardFont());
    }

	public boolean isVisualizationNecessary(RecordItem pRecordItem) {
		return true;
	}

	public void drawRecordItem(RecordItem pRecordItem) {
        
		getContext().getP5Applet().pushMatrix();
        applyRecordTrafo(pRecordItem, getContext().getCfg().getScreenHeight() / 400f);
		
    	PImage tTextureImage = 
    		getContext().getTextureCacheManager().getArtworkTextureAsync(pRecordItem.getPlaylistEntry());

        if (tTextureImage != null) {
            getContext().getP5Applet().beginShape();
            getContext().getP5Applet().tint(pRecordItem.getTransformation().getAlphaValue() * 255);
            getContext().getP5Applet().image(tTextureImage, -50, -50, 100, 100);
            getContext().getP5Applet().endShape();
        }
        
        if (DEBUG_MODE_ENABLED) {
        	drawDebugInfo(pRecordItem);
        }
        
        getContext().getP5Applet().popMatrix();
        
    }
	
	public void drawDebugInfo(RecordItem pRecordItem) {
		
		getContext().getP5Applet().pushMatrix();
		if (pRecordItem.isSelected()) {
        	getContext().getP5Applet().fill(155,100,100);
        } else {
        	getContext().getP5Applet().fill(155,155,155);
        }
        
		getContext().getP5Applet().textFont(debugFont);
        getContext().getP5Applet().text(
        		pRecordItem.toString(), 
                -25, 65, 
                100, 20,
                5);
        getContext().getP5Applet().scale(0.5f);
		getContext().getP5Applet().text(
        		pRecordItem.getPlaylistEntry().toString(),
                -100, 110, 
                200, 20,
                5);
        getContext().getP5Applet().textFont(defaultFont);
        
        getContext().getP5Applet().popMatrix();
		
	}

}
